<?php 
defined('IN_CGS') or die ('Restricted Access');
/**
 * @author longhoangvnn@gmail.com
 * @desc Các function sử dugj chung cho hệ thống
 */

class CgsFunc {
	
	/**
	 * Create new dir
	 * @param string $path
	 */
	static function createDir($path='', $is_create_sub=true, $mod='777', &$msg='') {
		if (file_exists($path)) return true;
		
		$path = str_replace('/', DS, $path);
		$path = str_replace('\\', DS, $path);
		if ($is_create_sub !== true) {
			if (@mkdir($path)) {
				@$exec = exec("chmod {$mod} {$path}");
				return true;
			} else {
				return false;
			}
		} else {
			$subs = explode(DS, $path, -1);
			$subpath = implode(DS, $subs);
			if (file_exists($subpath)) {
				if (@mkdir($path)) {
					$msg.= "- Created is folder [{$path}]\n";
					@$exec = exec("chmod {$mod} {$path}");
					if ($exec) {
						$msg.= "&nbsp; <b>chmod {$mod} {$path}</b>: is successfull\n";
					} else {
						$msg.= "&nbsp; <b>chmod {$mod} {$path}</b>: is not successfull\n";
					}
					return true;
				} else {
					return false;
				}
			} else {
				if ( self::createDir($subpath, true, $mod, $msg) ) {
					if (@mkdir($path)) {
						$msg.= "- Created is folder [{$path}]\n";
						@$exec = exec("chmod {$mod} {$path}");
						if ($exec) {
							$msg.= "&nbsp; <b>chmod {$mod} {$path}</b>: is successfull\n";
						} else {
							$msg.= "&nbsp; <b>chmod {$mod} {$path}</b>: is not successfull\n";
						}
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}
			}
			
		}
		
		//return false;
	}
}